<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class User extends Model
{
    protected $table = 'user';
    //public $timestamps = false;
    protected $fillable = [
        'realname',
        'sex',
        'department',
        'position',
        'phone',
        'status',
        'wechat',
        'number',
        'user_type',
        'openid',
        'created_at',
        'updated_at'
    ];

    const STAFF = 1;
    const USER = 2;
    const USER_TYPE = [
        self::STAFF => '员工',
        self::USER => '用户',
    ];

    const STATUS_SUCCESS = 1;
    const STATUS_FAILED = 2;
    const USER_STATUS = [
        self::STATUS_SUCCESS => '在职',
        self::STATUS_FAILED => '离职',
    ];

    const SEX_MAN = 1;
    const SEX_WOMAN = 2;
    const SEX_TYPE = [
        self::SEX_MAN => '男',
        self::SEX_WOMAN => '女',
    ];


    public function userAccountInfo()
    {
        return $this->hasOne('App\Models\UserAccount', 'user_id', 'id');
    }

    public function account()
    {
        return $this->hasOne('App\Models\UserAccount', 'user_id', 'id');
    }

    public function userDepartment()
    {
        return $this->hasOne('App\Models\UserDepartment', 'id', 'department')->withDefault([
            'id' => '',
            'number' => '',
            'department_name' => '',
        ]);
    }

    public function userPosition()
    {
        return $this->hasOne('App\Models\UserPosition', 'id', 'position')->withDefault([
            'id' => '',
            'number' => '',
            'position_name' => '',
            'department_id' => '',
        ]);
    }

    public function userHasOrder()
    {
        return $this->hasMany('App\Models\Order', 'user_id', 'id');
    }

    public function provinceInfo(){
        return $this->hasOne('App\Models\Province','code','province')->select('code','name')->withDefault([
           'code' => -1,
           'name' => '',
        ]);
    }

    public function cityInfo(){
        return $this->hasOne('App\Models\City','code','city')->select('code','name')->withDefault([
           'code' => -1,
           'name' => '',
        ]);
    }

    public function userHasInvite()
    {
        return $this->hasMany('App\Models\UserInvite', 'inviter_user_id', 'id');
    }

    public function inviteUserInfo(){
        return $this->hasOne('App\Models\User','id','inviter_user_id');
    }



}
